package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.ruoyi.db.domain.system.PcRaise;
import com.ruoyi.db.domain.system.PcRaiseConfig;
import com.ruoyi.db.mapper.system.PcRaiseConfigMapper;
import com.ruoyi.db.mapper.system.PcRaiseMapper;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.service.IPcRaiseConfigService;

import javax.transaction.Transactional;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-08-05
 */
@Service
@Transactional(rollbackOn = Exception.class)
public class PcRaiseConfigServiceImpl implements IPcRaiseConfigService 
{
    @Autowired
    private PcRaiseConfigMapper pcRaiseConfigMapper;
    @Autowired
    private PcRaiseMapper pcRaiseMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public PcRaiseConfig selectPcRaiseConfigById(Long id)
    {
        return pcRaiseConfigMapper.selectPcRaiseConfigById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param pcRaiseConfig 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<PcRaiseConfig> selectPcRaiseConfigList(PcRaiseConfig pcRaiseConfig)
    {
        List<PcRaiseConfig> configs = pcRaiseConfigMapper.selectPcRaiseConfigList(pcRaiseConfig);
        for (PcRaiseConfig config:configs) {
            config.setEarnRate(config.getEarnRate().multiply(new BigDecimal(100)));
            config.setAmountAddRate(config.getAmountAddRate().multiply(new BigDecimal(100)));
            config.setLimitAddRate(config.getLimitAddRate().multiply(new BigDecimal(100)));
        }
        return configs;
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param pcRaiseConfig 【请填写功能名称】
     * @return 结果
     */
    @Override
    public void insertPcRaiseConfig(PcRaiseConfig pcRaiseConfig)
    {
        pcRaiseConfig.setEarnRate(pcRaiseConfig.getEarnRate().divide(new BigDecimal(100)));
        pcRaiseConfig.setLimitAddRate(pcRaiseConfig.getLimitAddRate().divide(new BigDecimal(100)));
        pcRaiseConfig.setAmountAddRate(pcRaiseConfig.getAmountAddRate().divide(new BigDecimal(100)));
        pcRaiseConfig.setCreateTime(new Date());
        pcRaiseConfig.setIsDelete(0);
        pcRaiseConfig.setIsOver(0);
         pcRaiseConfigMapper.insertPcRaiseConfig(pcRaiseConfig);
         addNewRaiseByConfig();
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param pcRaiseConfig 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updatePcRaiseConfig(PcRaiseConfig pcRaiseConfig)
    {
        pcRaiseConfig.setEarnRate(pcRaiseConfig.getEarnRate().divide(new BigDecimal(100)));
        pcRaiseConfig.setLimitAddRate(pcRaiseConfig.getLimitAddRate().divide(new BigDecimal(100)));
        pcRaiseConfig.setAmountAddRate(pcRaiseConfig.getAmountAddRate().divide(new BigDecimal(100)));
        return pcRaiseConfigMapper.updatePcRaiseConfig(pcRaiseConfig);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deletePcRaiseConfigByIds(Long[] ids)
    {
        return pcRaiseConfigMapper.deletePcRaiseConfigByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deletePcRaiseConfigById(Long id)
    {
        return pcRaiseConfigMapper.deletePcRaiseConfigById(id);
    }

    @Override
    public void addNewRaiseByConfig() {
        List<PcRaiseConfig> configs = pcRaiseConfigMapper.selectPcRaiseConfigListAble();
        //循环活动配置表
        for (PcRaiseConfig pcRaiseConfig:configs) {
            PcRaise sRaise = new PcRaise();
            sRaise.setRaiseType(pcRaiseConfig.getRaiseType().longValue());
            List<PcRaise> raises = pcRaiseMapper.selectPcRaiseListByTime(sRaise);
            if(raises.size()==0){
                    //如果一开始活动表没数据,根据活动类型添加相应的三条数据
                    //第一条
                    PcRaise insRaise = new PcRaise();
                    insRaise.setRaiseName(pcRaiseConfig.getRaiseName());
                    insRaise.setStartTime(pcRaiseConfig.getStartTime());
                    insRaise.setContinueTime(addDateTime(pcRaiseConfig.getStartTime(), pcRaiseConfig.getContinueHour().intValue()));
                    insRaise.setEarnRate(pcRaiseConfig.getEarnRate());
                    insRaise.setMinTakeAmount(pcRaiseConfig.getMinTakeAmount());
                    insRaise.setMaxTakeAmount(pcRaiseConfig.getMaxTakeAmount());
                    insRaise.setOrderTime(addDateTime(pcRaiseConfig.getStartTime(), -pcRaiseConfig.getOrderHour().intValue()));
                    insRaise.setCreateTime(new Date());
                    BigDecimal total =pcRaiseConfig.getTotalAmount();
                    total =new BigDecimal(Math.round(total.doubleValue()));
                    insRaise.setTotalAmount(total);
                    insRaise.setIsSuccess(0);
                    insRaise.setRaiseType(pcRaiseConfig.getRaiseType().longValue());
                    insRaise.setFirstOrderTime(addDateTime(insRaise.getOrderTime(), pcRaiseConfig.getFirstOrderHour().intValue()));
                    insRaise.setIsAble(1);
                    insRaise.setIsOver(0);
                    insRaise.setRunCount(1l);
                    insRaise.setListCount(1l);
                    pcRaiseMapper.insertPcRaise(insRaise);
                    //第二条
                    insRaise.setStartTime(addDateTime(insRaise.getStartTime(), pcRaiseConfig.getNextHour().intValue()));
                    insRaise.setContinueTime(addDateTime(insRaise.getStartTime(), pcRaiseConfig.getContinueHour().intValue()));
                    insRaise.setEarnRate(pcRaiseConfig.getEarnRate());
                    insRaise.setMinTakeAmount(insRaise.getMinTakeAmount().
                            add(pcRaiseConfig.getLimitAddRate().multiply(insRaise.getMinTakeAmount()).setScale( 2, BigDecimal.ROUND_HALF_UP )));
                    insRaise.setMaxTakeAmount(insRaise.getMaxTakeAmount().
                            add(pcRaiseConfig.getLimitAddRate().multiply(insRaise.getMaxTakeAmount()).setScale( 2, BigDecimal.ROUND_HALF_UP )));
                    insRaise.setOrderTime(addDateTime(insRaise.getStartTime(), -pcRaiseConfig.getOrderHour().intValue()));
                    insRaise.setCreateTime(new Date());
                BigDecimal total1 =insRaise.getTotalAmount().
                        add(pcRaiseConfig.getAmountAddRate().multiply(insRaise.getTotalAmount()).setScale( 2, BigDecimal.ROUND_HALF_UP ));
                    total1 =new BigDecimal(Math.round(total1.doubleValue()));
                    insRaise.setTotalAmount(total1);
                    insRaise.setIsSuccess(0);
                    insRaise.setRaiseType(pcRaiseConfig.getRaiseType().longValue());
                    insRaise.setFirstOrderTime(addDateTime(insRaise.getOrderTime(), pcRaiseConfig.getFirstOrderHour().intValue()));
                    insRaise.setIsAble(1);
                    insRaise.setIsOver(0);
                    insRaise.setRunCount(1l);
                    insRaise.setListCount(2l);
                    pcRaiseMapper.insertPcRaise(insRaise);
                    //第三条
                    insRaise.setStartTime(addDateTime(insRaise.getStartTime(), pcRaiseConfig.getNextHour().intValue()));
                    insRaise.setContinueTime(addDateTime(insRaise.getStartTime(), pcRaiseConfig.getContinueHour().intValue()));
                    insRaise.setEarnRate(pcRaiseConfig.getEarnRate());
                    insRaise.setMinTakeAmount(insRaise.getMinTakeAmount().
                            add(pcRaiseConfig.getLimitAddRate().multiply(insRaise.getMinTakeAmount()).setScale( 2, BigDecimal.ROUND_HALF_UP )));
                    insRaise.setMaxTakeAmount(insRaise.getMaxTakeAmount().
                            add(pcRaiseConfig.getLimitAddRate().multiply(insRaise.getMaxTakeAmount()).setScale( 2, BigDecimal.ROUND_HALF_UP )));
                    insRaise.setOrderTime(addDateTime(insRaise.getStartTime(), -pcRaiseConfig.getOrderHour().intValue()));
                    insRaise.setCreateTime(new Date());
                BigDecimal total2 = insRaise.getTotalAmount().
                        add(pcRaiseConfig.getAmountAddRate().multiply(insRaise.getTotalAmount()).setScale( 2, BigDecimal.ROUND_HALF_UP ));
                total2 =new BigDecimal(Math.round(total2.doubleValue()));
                    insRaise.setTotalAmount(total2);
                    insRaise.setIsSuccess(0);
                    insRaise.setRaiseType(pcRaiseConfig.getRaiseType().longValue());
                    insRaise.setFirstOrderTime(addDateTime(insRaise.getOrderTime(), pcRaiseConfig.getFirstOrderHour().intValue()));
                    insRaise.setIsAble(1);
                    insRaise.setIsOver(0);
                    insRaise.setRunCount(1l);
                    insRaise.setListCount(3l);
                    pcRaiseMapper.insertPcRaise(insRaise);
            }else{
                //同一类型活动正在进行的永远只有3个可用的。
                //根据第一条活动记录是否可用就可判断该条活动后面活动是否可用
                PcRaise oRaise = new PcRaise();
                oRaise.setRaiseType(pcRaiseConfig.getRaiseType().longValue());
                oRaise.setIsOver(0);
                List<PcRaise> raisesO = pcRaiseMapper.selectPcRaiseListByTime(oRaise);
                if(raisesO.size()>=3&&raisesO.get(0).getIsAble()==1){
                    continue;
                }
                //如果该期活动没有被无效化,在最后一期活动基础上添加一条新的数据
                //查询上一期数据,根据上一期数据添加轮回数,期数
                PcRaise lRaise = new PcRaise();
                lRaise.setRaiseType(pcRaiseConfig.getRaiseType().longValue());
                lRaise.setStartTime(raisesO.get(0).getStartTime());
                PcRaise raiseL = pcRaiseMapper.selectLastByType(lRaise);
                if(raisesO.get(0).getIsAble()==1){
                    PcRaise pcRaiseR =  raisesO.get(raisesO.size()-1);
                    PcRaise insRaise =  new PcRaise();
                    insRaise.setRaiseType(pcRaiseR.getRaiseType());
                    insRaise.setRaiseName(pcRaiseR.getRaiseName());
                    insRaise.setIsSuccess(0);
                    insRaise.setCreateTime(new Date());
                    insRaise.setStartTime(addDateTime(pcRaiseR.getStartTime(),pcRaiseConfig.getNextHour().intValue()));
                    insRaise.setContinueTime(addDateTime(insRaise.getStartTime(),pcRaiseConfig.getContinueHour().intValue()));
                    insRaise.setEarnRate(pcRaiseConfig.getEarnRate());
                    insRaise.setMinTakeAmount(pcRaiseR.getMinTakeAmount().add(pcRaiseConfig.getLimitAddRate().multiply(pcRaiseR.getMinTakeAmount()).setScale( 2, BigDecimal.ROUND_HALF_UP )));
                    insRaise.setMaxTakeAmount(pcRaiseR.getMaxTakeAmount().add(pcRaiseConfig.getLimitAddRate().multiply(pcRaiseR.getMaxTakeAmount()).setScale( 2, BigDecimal.ROUND_HALF_UP )));
                    insRaise.setOrderTime(addDateTime(insRaise.getStartTime(), -pcRaiseConfig.getOrderHour().intValue()));
                    BigDecimal total =pcRaiseR.getTotalAmount().add(pcRaiseConfig.getAmountAddRate().multiply(pcRaiseR.getTotalAmount()).setScale( 2, BigDecimal.ROUND_HALF_UP ));
                    total =new BigDecimal(Math.round(total.doubleValue()));
                    insRaise.setTotalAmount(total);
                    insRaise.setFirstOrderTime(addDateTime(insRaise.getOrderTime(),pcRaiseConfig.getFirstOrderHour().intValue()));
                    insRaise.setIsAble(1);
                    insRaise.setIsOver(0);
                    insRaise.setRunCount(raiseL.getRunCount());
                    insRaise.setListCount(raiseL.getListCount()+1);
                    insRaise.setRaiseType(pcRaiseConfig.getRaiseType().longValue());
                    pcRaiseMapper.insertPcRaise(insRaise);
                }else{
                    //如果该期活动被无效化,初始化该期活动数据,并且再根据初始化的活动基础上添加一条新的数据
                    //如果一开始活动表没数据,根据活动类型添加相应的三条数据
                    //第一条
                    PcRaise insRaise = new PcRaise();
                    insRaise.setRaiseName(pcRaiseConfig.getRaiseName());
                    insRaise.setStartTime(pcRaiseConfig.getStartTime());
                    insRaise.setContinueTime(addDateTime(pcRaiseConfig.getStartTime(), pcRaiseConfig.getContinueHour().intValue()));
                    insRaise.setEarnRate(pcRaiseConfig.getEarnRate());
                    insRaise.setMinTakeAmount(pcRaiseConfig.getMinTakeAmount());
                    insRaise.setMaxTakeAmount(pcRaiseConfig.getMaxTakeAmount());
                    insRaise.setOrderTime(addDateTime(pcRaiseConfig.getStartTime(), -pcRaiseConfig.getOrderHour().intValue()));
                    insRaise.setCreateTime(new Date());
                    BigDecimal total =pcRaiseConfig.getTotalAmount();
                    total =new BigDecimal(Math.round(total.doubleValue()));
                    insRaise.setTotalAmount(total);
                    insRaise.setIsSuccess(0);
                    insRaise.setRaiseType(pcRaiseConfig.getRaiseType().longValue());
                    insRaise.setFirstOrderTime(addDateTime(insRaise.getOrderTime(), pcRaiseConfig.getFirstOrderHour().intValue()));
                    insRaise.setIsAble(1);
                    insRaise.setIsOver(0);
                    insRaise.setRunCount(1l);
                    insRaise.setListCount(1l);
                    pcRaiseMapper.insertPcRaise(insRaise);
                    //第二条
                    insRaise.setStartTime(addDateTime(insRaise.getStartTime(), pcRaiseConfig.getNextHour().intValue()));
                    insRaise.setContinueTime(addDateTime(insRaise.getStartTime(), pcRaiseConfig.getContinueHour().intValue()));
                    insRaise.setEarnRate(pcRaiseConfig.getEarnRate());
                    insRaise.setMinTakeAmount(insRaise.getMinTakeAmount().
                            add(pcRaiseConfig.getLimitAddRate().multiply(insRaise.getMinTakeAmount()).setScale( 2, BigDecimal.ROUND_HALF_UP )));
                    insRaise.setMaxTakeAmount(insRaise.getMaxTakeAmount().
                            add(pcRaiseConfig.getLimitAddRate().multiply(insRaise.getMaxTakeAmount()).setScale( 2, BigDecimal.ROUND_HALF_UP )));
                    insRaise.setOrderTime(addDateTime(insRaise.getStartTime(), -pcRaiseConfig.getOrderHour().intValue()));
                    insRaise.setCreateTime(new Date());
                    BigDecimal total1 =insRaise.getTotalAmount().
                            add(pcRaiseConfig.getAmountAddRate().multiply(insRaise.getTotalAmount()).setScale( 2, BigDecimal.ROUND_HALF_UP ));
                    total1 =new BigDecimal(Math.round(total1.doubleValue()));
                    insRaise.setTotalAmount(total1);
                    insRaise.setIsSuccess(0);
                    insRaise.setRaiseType(pcRaiseConfig.getRaiseType().longValue());
                    insRaise.setFirstOrderTime(addDateTime(insRaise.getOrderTime(), pcRaiseConfig.getFirstOrderHour().intValue()));
                    insRaise.setIsAble(1);
                    insRaise.setIsOver(0);
                    insRaise.setRunCount(1l);
                    insRaise.setListCount(2l);
                    pcRaiseMapper.insertPcRaise(insRaise);
                    //第三条
                    insRaise.setStartTime(addDateTime(insRaise.getStartTime(), pcRaiseConfig.getNextHour().intValue()));
                    insRaise.setContinueTime(addDateTime(insRaise.getStartTime(), pcRaiseConfig.getContinueHour().intValue()));
                    insRaise.setEarnRate(pcRaiseConfig.getEarnRate());
                    insRaise.setMinTakeAmount(insRaise.getMinTakeAmount().
                            add(pcRaiseConfig.getLimitAddRate().multiply(insRaise.getMinTakeAmount()).setScale( 2, BigDecimal.ROUND_HALF_UP )));
                    insRaise.setMaxTakeAmount(insRaise.getMaxTakeAmount().
                            add(pcRaiseConfig.getLimitAddRate().multiply(insRaise.getMaxTakeAmount()).setScale( 2, BigDecimal.ROUND_HALF_UP )));
                    insRaise.setOrderTime(addDateTime(insRaise.getStartTime(), -pcRaiseConfig.getOrderHour().intValue()));
                    insRaise.setCreateTime(new Date());
                    BigDecimal total2 = insRaise.getTotalAmount().
                            add(pcRaiseConfig.getAmountAddRate().multiply(insRaise.getTotalAmount()).setScale( 2, BigDecimal.ROUND_HALF_UP ));
                    total2 =new BigDecimal(Math.round(total2.doubleValue()));
                    insRaise.setTotalAmount(total2);
                    insRaise.setIsSuccess(0);
                    insRaise.setRaiseType(pcRaiseConfig.getRaiseType().longValue());
                    insRaise.setFirstOrderTime(addDateTime(insRaise.getOrderTime(), pcRaiseConfig.getFirstOrderHour().intValue()));
                    insRaise.setIsAble(1);
                    insRaise.setIsOver(0);
                    insRaise.setRunCount(1l);
                    insRaise.setListCount(3l);
                    pcRaiseMapper.insertPcRaise(insRaise);
                }

            }
        }

    }
    /**
     * 给时间date加上天数，小时，分钟
     * @param date   //原本时间
     * @param hour   //要加的小时
     */
    public static Date addDateTime(Date date, int minute) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MINUTE, minute);// 24小时制 ,加小时
        date = cal.getTime();
        System.out.println(format.format(date));
        return  date;
    }



}
